package com.centaline.androidsalesblog.widgets.dropdown;

import android.content.Context;
import android.support.v7.widget.AppCompatEditText;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.RelativeLayout;

import com.centaline.androidsalesblog.R;
import com.centaline.androidsalesblog.model.businissobject.DropBo;
import com.jakewharton.rxbinding.view.RxView;
import com.jakewharton.rxbinding.widget.RxTextView;

import java.util.ArrayList;
import java.util.Locale;
import java.util.concurrent.TimeUnit;

import rx.Observable;
import rx.functions.Action1;
import rx.functions.Func2;

/**
 * 二手总价
 * Created by guilin on 16/1/26.
 */
public class SalePriceDrop extends AbsDrop {

    private int priceUnit;//单位0:万元;1:元
    private RelativeLayout rl_parent;
    private ListView listView;
    private AppCompatEditText aet_min, aet_max;
    private Button btn_confirm;
    private DropTextAdapter dropTextAdapter;

    private Observable<CharSequence> minObservable, maxObservable;

    public SalePriceDrop(final View anchor, Context context) {
        super(anchor, context);
        View view = LayoutInflater.from(context).inflate(R.layout.layout_drop_price, null);
        rl_parent = (RelativeLayout) view.findViewById(R.id.rl_parent);
        rl_parent.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        listView = (ListView) view.findViewById(R.id.listView);
        aet_min = (AppCompatEditText) view.findViewById(R.id.aet_min);
        aet_max = (AppCompatEditText) view.findViewById(R.id.aet_max);
        btn_confirm = (Button) view.findViewById(R.id.btn_confirm);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                dropTextAdapter.select(position);
                aet_min.setText("");
                aet_max.setText("");
                dropComplete(false, 30, arrayList.get(position));
            }
        });
        setPriceUnit(0);
        minObservable = RxTextView.textChanges(aet_min);
        maxObservable = RxTextView.textChanges(aet_max);
        Observable.combineLatest(minObservable, maxObservable, new Func2<CharSequence, CharSequence, Boolean>() {
            @Override
            public Boolean call(CharSequence charSequence, CharSequence charSequence2) {
                if (TextUtils.isEmpty(charSequence) && TextUtils.isEmpty(charSequence2)) {
                    return false;
                } else if (TextUtils.isEmpty(charSequence) || TextUtils.isEmpty(charSequence2)||charSequence.toString().startsWith(".")||charSequence2.toString().startsWith(".")) {
                    return false;
                } else {
                    double min = Double.parseDouble(charSequence.toString());
                    double max = Double.parseDouble(charSequence2.toString());
                    return min < max;
                }
            }
        }).subscribe(new Action1<Boolean>() {
            @Override
            public void call(Boolean aBoolean) {
                btn_confirm.setEnabled(aBoolean);
            }
        });
        RxView.clicks(btn_confirm)
                .throttleFirst(1, TimeUnit.SECONDS)
                .subscribe(new Action1<Void>() {
                    @Override
                    public void call(Void aVoid) {
                        dropTextAdapter.select(-1);
                        String minString = aet_min.getText().toString();
                        String maxString = aet_max.getText().toString();
                        DropBo dropBo1, dropBo2;
                        dropBo1 = new DropBo(30);
                        dropBo1.setValue(TextUtils.isEmpty(minString) ? "" :
                                priceUnit == 1 ? minString :
                                        String.format(Locale.CHINA, "%.0f", Double.parseDouble(minString) * 10000));
                        dropBo1.setText(minString);
                        dropBo2 = new DropBo(30);
                        dropBo2.setValue(TextUtils.isEmpty(maxString) ? "" :
                                priceUnit == 1 ? maxString :
                                        String.format(Locale.CHINA, "%.0f", Double.parseDouble(maxString) * 10000));
                        dropBo2.setText(maxString);
                        dropComplete(false, 30, dropBo1, dropBo2);
                    }
                });

        dropTextAdapter = new DropTextAdapter(arrayList);
        listView.setAdapter(dropTextAdapter);
        initPopWindow(view);
    }

    @Override
    public void init(ArrayList<DropBo> dropBos) {
        arrayList.clear();
        arrayList.addAll(dropBos);
        dropTextAdapter.notifyDataSetChanged();
    }

    public void setPriceUnit(int priceUnit) {
        this.priceUnit = priceUnit;
        aet_min.setHint(priceUnit == 1 ? "最低(元)" : "最低(万元)");
        aet_max.setHint(priceUnit == 1 ? "最高(元)" : "最高(万元)");
    }

    public void setMaxAndMin(String max, String min) {
        aet_max.setText(max);
        aet_min.setText(min);
    }

    @Override
    public void onDismiss() {

    }
}
